package org.vergil.core.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.vergil.core.dto.User;
import org.vergil.core.dao.dev.UserRepository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vergil on 2/26/17.
 */
@Service("userService")
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CacheManager cacheManager;

    private Logger logger = LoggerFactory.getLogger(UserService.class);

    @Cacheable(cacheNames = "users")
    public List<User> getUsers(){
        logger.info("getUsers for userService");
        Map<String, Object> param = new HashMap();
        return userRepository.getUserList(param);
    }

    @CachePut(cacheNames = "users")
    public User saveUser(User user){
        logger.info("save user at userRepository !");
        return userRepository.save(user);
    }

//    @PostConstruct
//    public void setUp(){
//        logger.info("setUp for userService");
//        Cache cache = cacheManager.getCache("users");
//
//        Map<String, Object> param = new HashMap();
//        List<User> users = userRepository.getUserList(param);
//        cache.put(SimpleKey.EMPTY,users);
////        for (User user : users){
////            cache.put(user.getId(),user);
////        }
//    }
//
}
